package com.data.collect.android.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;

import com.data.collect.configer.Constance;
import com.data.collect.tool.FileTool;
import com.data.collect.tool.Logger;
import com.data.collect.tool.StringTools;
import com.data.collect.tool.cach.PreferceManager;

import org.json.JSONObject;


/**
 * Created by wanggenlin on 17-6-8.
 */
public class PSdkReceiver extends BroadcastReceiver {
    static String airPlanChangeKey = "airPlaneChangeKey";

    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(Constance.ServiceAction));
        switch (intent.getAction()) {
            case Intent.ACTION_AIRPLANE_MODE_CHANGED:
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                    int state = bundle.getInt("state");
                    if (state == 0x00) {
                        Logger.e("airPlan  state = 0x00");
                        PreferceManager.getInsance().saveValueBYkey(airPlanChangeKey, System.currentTimeMillis() / 1000 + "", context);
                    }
                }

                printText("air","");
                break;
            case Intent.ACTION_BATTERY_CHANGED:
                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
                int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
                Logger.e("电压：" + voltage / 1000 + "." + voltage % 1000 + "V");
                printText("batteryV",voltage / 1000 + "." + voltage % 1000 + "V");

                int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
                Logger.e("温度：" + temperature / 10 + "." + temperature % 10 + "℃");
                printText("batteryT",temperature / 10 + "." + temperature % 10 + "℃");

                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
                int levelPercent = (int) (((float) level / scale) * 100);
                Logger.e("电量：" + levelPercent + "%");
                printText("batteryP",levelPercent + "%");
                String strStatus = "";
                switch (status) {
                    case BatteryManager.BATTERY_STATUS_CHARGING:
                        strStatus = "充电中……";
                        printText("batteryCharge","Charging");
                        break;
                    case BatteryManager.BATTERY_STATUS_DISCHARGING:
                        strStatus = "放电中……";
                        printText("batteryCharge","unCharging");
                        break;
                    case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                        strStatus = "未充电";
                        printText("batteryCharge","unCharge");
                        break;
                    case BatteryManager.BATTERY_STATUS_FULL:
                        strStatus = "充电完成";
                        printText("batteryCharge","Charged");
                        break;
                }
                Logger.e("状态：" + strStatus);
            break;
        case Intent.ACTION_POWER_CONNECTED://接通电源
            Logger.e("接通电源");
            printText("powerConnect","on");

            break;
        case Intent.ACTION_POWER_DISCONNECTED://拔出电源
            printText("powerConnect","off");
            Logger.e("拔出电源");
            break;
        case Intent.ACTION_CAMERA_BUTTON:
            Logger.e("camer button");
            printText("camera","click");
            break;
        case Intent.ACTION_BOOT_COMPLETED:
            printText("boot","on");
            break;
        case Intent.ACTION_SCREEN_ON:
             printText("screen","on");
             Logger.e("screen on");
            break;
        case Intent.ACTION_SCREEN_OFF:
            printText("screen","off");
            Logger.e("screen off");
            break;
        case Intent.ACTION_USER_PRESENT:
             printText("screen","present");
             Logger.e("screen present");
             break;
        case Intent.ACTION_CONFIGURATION_CHANGED:
             printText("system","chanage");
             Logger.e ("system chanage");
             break;
        case Intent.ACTION_LOCALE_CHANGED:
                printText("system","chanage");
                Logger.e ("system chanage");
              break;
        case Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE:
                printText("app2sd","complete");
                Logger.e ("app2sd complete");
             break;
        case Intent.ACTION_GTALK_SERVICE_CONNECTED:
                printText("gtalk","on");
                Logger.e ("gtalk on");
             break;
        case Intent.ACTION_GTALK_SERVICE_DISCONNECTED:
                printText("gtalk","off");
                Logger.e ("gtalk off");
               break;
        case Intent.ACTION_MEDIA_BAD_REMOVAL:
                printText("sd","remove");
                Logger.e ("sd remove");
               break;
        case Intent.ACTION_WALLPAPER_CHANGED:
                printText("wall","chage");
                Logger.e ("wall chage");
               break;

        case Intent.ACTION_PACKAGE_REMOVED:
                printText("pack","remove$"+intent.getDataString());
                Logger.e ("pack remove$"+intent.getDataString());
               break;

        case Intent.ACTION_PACKAGE_ADDED:
                printText("pack","add$"+intent.getDataString());
                Logger.e ("pack add$"+intent.getDataString());
                break;

        default:
            break;

        }
        if (intent.getAction().equals("android.hardware.action.NEW_PICTURE")){
            printText("camera","click");
            Logger.e("camer button");
        }
    }

    private  void printText(String key,String content){
        try {
            JSONObject js = new JSONObject();
            js.put("t","b");
            js.put("n",key);
            js.put("t",System.currentTimeMillis()/1000);
            if (!StringTools.isNullOrEmpty(content)){
                js.put("v",content);
            }
            FileTool.getInstance().writeDayLog(js.toString());
        }catch (Exception e){

        }
    }
}
